package com.coocaa.fileexplore.pages.home;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.coocaa.fileexplore.R;
import com.coocaa.fileexplore.pages.home.data.FileData;

import java.util.List;

/**
 * Created by xzxiao on 2017/4/12.
 */

class FileAdapter extends ArrayAdapter<FileData> {
    private int resId;
    private Context context;

    public FileAdapter(Context context, int textViewResourceId, List<FileData> objects) {
        super(context, textViewResourceId, objects);
        resId = textViewResourceId;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FileData data = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(resId, null);
        } else {
            view = convertView;
        }
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView size = (TextView) view.findViewById(R.id.size);
        TextView type = (TextView) view.findViewById(R.id.type);

        if (data != null) {
            name.setText(data.name);
            if (!data.name.equals("..")) {
                if (data.size < 0)
                    size.setText("loading...");
                else
                    size.setText(String.valueOf(data.size) + " MB");
                type.setText(data.type == 0 ? "目录  " : "文件  ");
            } else {
                size.setText("");
                type.setText("");
            }
            if (data.type == 0) {
                name.setTextColor(Color.WHITE);
                size.setTextColor(Color.WHITE);
                type.setTextColor(Color.WHITE);
            } else if (data.type == 1) {
                name.setTextColor(Color.parseColor("#98FB98"));
                size.setTextColor(Color.parseColor("#98FB98"));
                type.setTextColor(Color.parseColor("#98FB98"));
            }
        }
        return view;
    }
}
